package com.zb.order.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjWorkStatementTemplateFieldService;
import com.zb.service.ZjWorkStatementTemplateModuleService;
import com.zb.service.ZjWorkStatementTemplateService;
import com.zb.utils.TokenUtil;
import com.zb.vo.WorkStatementTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 工单结单模板表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-10-07
 */
@Api(tags = "工单结单模板类接口")
@RestController
@RequestMapping("/work/statement/template")
public class ZjWorkStatementTemplateController {
    @Resource
    private ZjWorkStatementTemplateService workStatementTemplateService;
    @Resource
    private ZjWorkStatementTemplateFieldService workStatementTemplateFieldService;
    @Resource
    private ZjWorkStatementTemplateModuleService workStatementTemplateModuleService;

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "工单设置-自定义模板-结单模板 - 新增或修改工单费用项目")
    @ApiOperation("新增或修改结单模板")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid WorkStatementTemplateVo templateVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workStatementTemplateService.save(loginInfo, templateVo);
        return Result.success(null == templateVo.getId()? "新增结单模板成功" : "修改结单模板成功");
    }

    @ApiOperation("获取结单模板列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody SelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取结单模板列表成功").data(workStatementTemplateService.getList(custAccId, selectVo));
    }
    @ApiOperation("根据ID获取结单模板详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "结单模板ID", required = true) Long id) {
        return Result.success("获取结单模板详情成功").data("row", workStatementTemplateService.getDetail(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.UPDATE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用结单模板")
    @ApiOperation("批量启用或禁用结单模板")
    @PutMapping("/disable")
    public Result disable(HttpServletRequest request, @RequestParam @ApiParam(value = "结单模板状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "结单模板ID集合", required = true) List<Long> idList) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workStatementTemplateService.updateDisabledBatchById(loginInfo, idList, status);
        return Result.success(status? "禁用结单模板成功" : "启用结单模板成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.DELETE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用结单模板")
    @ApiOperation("批量删除结单模板")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "结单模板ID集合", required = true) List<Long> idList) {
        workStatementTemplateService.deleteBatchById(idList);
        return Result.success("删除结单模板成功");
    }

    @ApiOperation("根据字段ID获取结单字段详情")
    @GetMapping("/getFieldDetail/{id}")
    public Result getFieldDetail(@PathVariable @ApiParam(value = "结单模板自定义字段ID", required = true) Long id) {
        return Result.success("获取结单字段详情成功").data("row", workStatementTemplateFieldService.getFieldDetail(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.UPDATE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用结单模板字段")
    @ApiOperation("批量启用或禁用结单模板字段")
    @PutMapping("/disableField")
    public Result disableField(@RequestParam @ApiParam(value = "结单字段状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "结单字段ID集合", required = true) List<Long> ids) {
        workStatementTemplateFieldService.updateDisableBatchById(ids, status);
        return Result.success(status? "结单字段禁用成功" : "结单字段启用成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.DELETE, remark = "工单设置-自定义模板-结单模板 - 批量删除结单模板字段")
    @ApiOperation("批量删除结单模板字段")
    @DeleteMapping("/deleteField")
    public Result deleteField(@RequestBody @ApiParam(value = "结单字段ID集合", required = true) List<Long> ids) {
        workStatementTemplateFieldService.deleteBatchById(ids);
        return Result.success("删除结单字段成功");
    }

    @ApiOperation("根据工单模块ID获取结单模块字段列表")
    @PostMapping("/getModuleField/{moduelId}")
    public Result getModuleField(@PathVariable @ApiParam(value = "工单模块ID", required = true) Long moduelId, @RequestBody SelectVo selectVo) {
        return Result.success("获取结单模块字段列表成功").data(workStatementTemplateService.getModuleField(moduelId, selectVo));
    }

    @ApiOperation("根据模块ID获取关联结单模板")
    @GetMapping("/getWorkStatementTemplateModule/{moduleId}")
    public Result getWorkStatementTemplateModule(@PathVariable @ApiParam(value = "工单模块ID", required = true) Long moduleId) {
        return Result.success("获取结单模板成功").data(PageUtil.rows(workStatementTemplateModuleService.getWorkStatementTemplateModuleByModuleId(moduleId)));
    }

    @ApiOperation("保存工单模块结单模板")
    @PostMapping("/saveOrderModuleFinishTemplate/{moduleId}")
    public Result saveOrderModuleFinishTemplate(HttpServletRequest request, @PathVariable @ApiParam(value = "工单模块ID", required = true) Long moduleId,
                                                @RequestBody @ApiParam(value = "结单模块ID", required = true) List<Long> workStatementTemplateIds) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        workStatementTemplateModuleService.saveOrderModuleFinishTemplate(custAccId, moduleId, workStatementTemplateIds);
        return Result.success("保存模块关联结单模板成功");
    }






}
